package com.example.ui_test.ui

import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.ui_test.R
import com.example.ui_test.adapter.ColorAdapter
import com.example.ui_test.bean.ColorBean
import com.kun.base.ui.AbsBaseActivity
import kotlinx.android.synthetic.main.activity_color.*
import java.util.*

/**
 *
 * @author zgl
 * 日期：2023/8/10
 * 时间：16:30
 *
 */
@Route(path = "/uiTest/ColorActivity")
class ColorActivity : AbsBaseActivity() {
    lateinit var colorAdapter: ColorAdapter

    val dataList = mutableListOf<MutableList<ColorBean>>()
    val rowCount = 16

    override fun getLayoutId(): Int {
        return R.layout.activity_color
    }

    override fun initViewModel() {
    }

    override fun initView() {
        colorAdapter = ColorAdapter(R.layout.item_color, null)
        val gridLayoutManager = GridLayoutManager(this, rowCount)
        rcl.layoutManager = gridLayoutManager
        rcl.adapter = colorAdapter

        btn_change.setOnClickListener {
            dataList[Random().nextInt(11)][Random().nextInt(11)].num = (Random().nextFloat() * 2000).toInt() / 10f
//            dataList[Random().nextInt(11)][Random().nextInt(11)].num = (Random().nextFloat() * 2000).toInt() / 10f
        }

        initData()

        changeAuto()
    }

    private fun changeAuto() {
        rcl.postDelayed({
            if (!isFinishing) {
                change()
                changeAuto()
            }
        }, 100)
    }

    /**
     * 初始化
     */
    private fun initData() {
        for (i in 0 until rowCount) {
            val cl = mutableListOf<ColorBean>()
            for (j in 0 until rowCount) {
                cl.add(ColorBean((Random().nextFloat() * 2000).toInt() / 10f, true))
            }
            dataList.add(cl)
        }

        dataList[11][11].isCanChange = false
        dataList[11][11].num = 200f
        dataList[12][12].isCanChange = false
        dataList[12][12].num = 200f
        dataList[0][0].isCanChange = false
        dataList[0][0].num = 0f

        val list = mutableListOf<ColorBean>()

        for (i in 0 until rowCount) {
            for (j in 0 until rowCount) {
                list.add(dataList[i][j])
            }
        }
        colorAdapter.setList(list)
    }

    fun change() {
        val deList = mutableListOf<MutableList<Float>>()
        for (i in dataList.indices) {
            val cl = mutableListOf<Float>()
            for (j in dataList[i].indices) {
                var count = 0
                var temp = 0f
                if (j > 0) {
                    count++
                    temp += (dataList[i][j - 1].num - dataList[i][j].num) / 3
                }
                if (i > 0) {
                    count++
                    temp += (dataList[i - 1][j].num - dataList[i][j].num) / 3
                }
                if (j < dataList[i].size - 1) {
                    count++
                    temp += (dataList[i][j + 1].num - dataList[i][j].num) / 3
                }
                if (i < dataList.size - 1) {
                    count++
                    temp += (dataList[i + 1][j].num - dataList[i][j].num) / 3
                }
                cl.add(if (temp != 0f) ((temp / count) * 10f).toInt() / 10f else 0f)
            }
            deList.add(cl)
        }

        for (i in dataList.indices) {
            for (j in dataList[i].indices) {
                if (dataList[i][j].isCanChange)
                    dataList[i][j].num += deList[i][j]
            }
        }

        val list = mutableListOf<ColorBean>()

        for (i in 0 until rowCount) {
            for (j in 0 until rowCount) {
                list.add(dataList[i][j])
            }
        }
        colorAdapter.setList(list)
    }
}